Completed
Pull Request — master (#99)
by
unknown
01:05
created

api_client-other.test.js ➔ describe(ꞌsend rawꞌ)   B

Complexity

Conditions 1
Paths 2

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 2
dl 0
loc 31
rs 8.8571
nop 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A api_client-other.test.js ➔ ... ➔ it(ꞌshould error decode failedꞌ) 0 8 1
A api_client-other.test.js ➔ ... ➔ it(ꞌshould report TX is already in blockchainꞌ) 0 8 1
1
/* jshint -W101, -W098 */
2
var blocktrail = require('../');
3
var assert = require('assert');
4
var crypto = require('crypto');
5
6
/**
7
 * @type APIClient
8
 */
9
var client = blocktrail.BlocktrailSDK({
10
    apiKey: process.env.BLOCKTRAIL_SDK_APIKEY || "EXAMPLE_BLOCKTRAIL_SDK_NODEJS_APIKEY",
11
    apiSecret: process.env.BLOCKTRAIL_SDK_APISECRET || "EXAMPLE_BLOCKTRAIL_SDK_NODEJS_APISECRET",
12
    btccom: false
13
});
14
15
describe('SDK general', function() {
16
    it('test Coin Value', function(cb) {
17
        assert.equal(blocktrail.toSatoshi(0.00000001), 1);
18
        assert.equal(blocktrail.toBTC(100000000), 1.0);
19
20
        assert.equal(blocktrail.toSatoshi(1.23456789), 123456789);
21
        assert.equal(blocktrail.toBTC(123456789), 1.23456789);
22
23
        cb();
24
    });
25
    it('test auth failure', function(cb) {
26
        var client = blocktrail.BlocktrailSDK({
27
            apiKey: "TESTKEY-FAIL",
28
            apiSecret: "TESTSECRET-FAIL"
29
        });
30
31
        client.address("1dice8EMZmqKvrGE4Qc9bUFf9PX3xaYDp", function(err, address) {
0 ignored issues
show
Unused Code introduced by
The parameter address is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
32
            assert.ok(err);
33
34
            cb();
35
        });
36
    });
37
});
38
39
describe('webhooks api', function() {
40
    var createdWebhooks = [];
41
    var cleanup = function(done) {
42
        client.allWebhooks({page:1, limit:500}, function(err, response) {
0 ignored issues
show
Unused Code introduced by
The parameter response is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter err is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
43
            //delete each webhook
44
            //var allWebhooks = response.data;
45
            if (!createdWebhooks.length) {
46
                done();
47
            }
48
            createdWebhooks.forEach(function(identifier) {
49
                client.deleteWebhook(identifier, function(err, response) {
0 ignored issues
show
Unused Code introduced by
The parameter err is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter response is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
50
                    createdWebhooks.splice(identifier, 1);
51
                    if (createdWebhooks.length === 0) {
52
                        done();
53
                    }
54
                });
55
            });
56
        });
57
    };
58
    before(function(done) {
59
        // runs before all tests in this block..cleanup any existing data that could conflict with the tests
60
        cleanup(done);
61
    });
62
    after(function(done) {
63
        //cleanup after all tests
64
        cleanup(done);
65
    });
66
67
    //create a custom (yet "random") identifier such to avoid conflicts when running multiple tests simultaneously
68
    var myIdentifier = crypto.randomBytes(24).toString('hex');
69
70
    // test cases
71
    it('create new webhook with custom identifier', function(done) {
72
        client.setupWebhook("https://www.blocktrail.com/webhook-test", myIdentifier, function(err, webhook) {
73
            assert.ifError(err);
74
            assert.equal(webhook.url, "https://www.blocktrail.com/webhook-test");
75
            assert.equal(webhook.identifier, myIdentifier);
76
            createdWebhooks.push(webhook.identifier);
77
            done();
78
        });
79
    });
80
81
    it('create new webhook with random identifier', function(done) {
82
        client.setupWebhook("https://www.blocktrail.com/webhook-test", function(err, webhook) {
83
            assert.ifError(err);
84
            assert.equal(webhook.url, "https://www.blocktrail.com/webhook-test");
85
            assert.ok(webhook.identifier);
86
            createdWebhooks.push(webhook.identifier);
87
            done();
88
        });
89
    });
90
91
    it('get all user webhooks', function(done) {
92
        client.allWebhooks(null, function(err, response) {
93
            assert.ifError(err);
94
            assert.ok('data' in response, "'data' key not in response");
95
            assert.ok('total' in response, "'total' key not in response");
96
            assert.ok(parseInt(response['total']) >= 2, "'total' does not match expected value");
97
            assert.ok(response['data'].length >= 2, "Count of webhooks returned is not equal to 2");
98
99
            assert.ok('url' in response['data'][0], "'url' key not in first webhook of response");
100
            assert.ok('url' in response['data'][1], "'url' key not in second webhook of response");
101
            done();
102
        });
103
    });
104
105
    it('get a single webhook', function(done) {
106
        client.getWebhook(createdWebhooks[0], function(err, response) {
107
            assert.ifError(err);
108
            assert.ok('url' in response, "'url' key not in response");
109
            assert.ok('identifier' in response, "'identifier' key not in response");
110
            assert.equal(response['url'], "https://www.blocktrail.com/webhook-test", "'url' does not match expected value");
111
            assert.equal(response['identifier'], myIdentifier, "'identifier' does not match expected value");
112
            done();
113
        });
114
    });
115
116
    it('delete a webhook', function(done) {
117
        client.deleteWebhook(createdWebhooks[0], function(err, response) {
118
            assert.ifError(err);
119
            assert.ok(response);
120
            done();
121
        });
122
    });
123
124
    it('update a webhook', function(done) {
125
        var newIdentifier = crypto.randomBytes(24).toString('hex');
126
        var newUrl = "https://www.blocktrail.com/new-webhook-url";
127
        client.updateWebhook(createdWebhooks[1], {identifier: newIdentifier, url: newUrl}, function(err, response) {
128
            assert.ifError(err);
129
            assert.ok('url' in response, "'url' key not in response");
130
            assert.ok('identifier' in response, "'identifier' key not in response");
131
            assert.equal(response['url'], newUrl, "'url' does not match expected value");
132
            assert.equal(response['identifier'], newIdentifier, "'identifier' does not match expected value");
133
134
            createdWebhooks[1] = newIdentifier;
135
            done();
136
        });
137
    });
138
139
    it('subscribe to address-transaction events', function(done) {
140
        var address = "1dice8EMZmqKvrGE4Qc9bUFf9PX3xaYDp";
141
        client.subscribeAddressTransactions(createdWebhooks[1], address, 2, function(err, response) {
142
            assert.ifError(err);
143
            assert.ok('event_type' in response, "'event_type' key not in response");
144
            assert.ok('address' in response, "'address' key not in response");
145
            assert.ok('confirmations' in response, "'confirmations' key not in response");
146
            assert.equal(response['event_type'], "address-transactions", "'event_type' does not match expected value");
147
            assert.equal(response['address'], address, "'address' does not match expected value");
148
            assert.equal(response['confirmations'], 2, "'confirmations' does not match expected value");
149
            done();
150
        });
151
    });
152
153
    it('subscribe to transaction event', function(done) {
154
        var transaction = "a0a87b1577d606b349cfded85c842bdc53b99bcd49614229a71804b46b1c27cc";
155
        client.subscribeTransaction(createdWebhooks[1], transaction, 2, function(err, response) {
156
            assert.ifError(err);
157
            assert.ok('event_type' in response, "'event_type' key not in response");
158
            assert.ok('address' in response, "'address' key not in response");
159
            assert.ok('confirmations' in response, "'confirmations' key not in response");
160
            assert.equal(response['event_type'], "transaction", "'event_type' does not match expected value");
161
            assert.equal(response['transaction'], transaction, "'transaction' does not match expected value");
162
            assert.equal(response['confirmations'], 2, "'confirmations' does not match expected value");
163
            done();
164
        });
165
    });
166
167
    it('subscribe to new block events', function(done) {
168
        client.subscribeNewBlocks(createdWebhooks[1], function(err, response) {
169
            assert.ifError(err);
170
            assert.ok('event_type' in response, "'event_type' key not in response");
171
            assert.ok('address' in response, "'address' key not in response");
172
            assert.ok('confirmations' in response, "'confirmations' key not in response");
173
            assert.equal(response['event_type'], "block", "'event_type' does not match expected value");
174
            assert.equal(response['address'], null, "'address' does not match expected value");
175
            assert.equal(response['confirmations'], null, "'confirmations' does not match expected value");
176
            done();
177
        });
178
    });
179
180
    it('batch subscribe to address-transaction events', function(done) {
181
        var batchData = [
182
            {
183
                'event_type': 'address-transactions',
184
                'address': '18FA8Tn54Hu8fjn7kkfAygPoGEJLHMbHzo',
185
                'confirmations': 1
186
            },
187
            {
188
                'address': '1LUCKYwD6V9JHVXAFEEjyQSD4Dj5GLXmte',
189
                'confirmations': 1
190
            },
191
            {
192
                'address': '1qMBuZnrmGoAc2MWyTnSgoLuWReDHNYyF'
193
            }
194
        ];
195
        client.batchSubscribeAddressTransactions(createdWebhooks[1], batchData, function(err, response) {
196
            assert.ifError(err);
197
            assert.ok(response);
198
            done();
199
        });
200
    });
201
202
    it('get webhook event subscriptions', function(done) {
203
        client.getWebhookEvents(createdWebhooks[1], function(err, response) {
204
            assert.ifError(err);
205
            assert.ok('data' in response, "'data' key not in response");
206
            assert.ok('total' in response, "'total' key not in response");
207
            assert.equal(parseInt(response['total']), 6, "'total' does not match expected value");
208
            assert.equal(response['data'].length, 6, "Count of event subscriptions returned is not equal to 2");
209
210
            assert.ok('event_type' in response['data'][0], "'event_type' key not in first event subscription of response");
211
212
            done();
213
        });
214
    });
215
216
    it('unsubscribe from address-transaction events', function(done) {
217
        var address = "1dice8EMZmqKvrGE4Qc9bUFf9PX3xaYDp";
218
        client.unsubscribeAddressTransactions(createdWebhooks[1], address, function(err, response) {
219
            assert.ifError(err);
220
            assert.ok(response);
221
            done();
222
        });
223
    });
224
225
    it('unsubscribe from new transaction events', function(done) {
226
        var transaction = "a0a87b1577d606b349cfded85c842bdc53b99bcd49614229a71804b46b1c27cc";
227
        client.unsubscribeTransaction(createdWebhooks[1], transaction, function(err, response) {
228
            assert.ifError(err);
229
            assert.ok(response);
230
            done();
231
        });
232
    });
233
234
    it('unsubscribe from new block events', function(done) {
235
        client.unsubscribeNewBlocks(createdWebhooks[1], function(err, response) {
236
            assert.ifError(err);
237
            assert.ok(response);
238
            done();
239
        });
240
    });
241
});
242
243
describe('market api', function() {
244
    it('should have a price', function(done) {
245
        client.price(function(err, price) {
246
            assert.ifError(err);
247
            assert.ok(price);
248
            assert.ok(price['USD']);
249
            done();
250
        });
251
    });
252
});
253
254
describe('verify message', function() {
255
    var address = "1F26pNMrywyZJdr22jErtKcjF8R3Ttt55G";
256
    var message = address;
257
    var signature = "H85WKpqtNZDrajOnYDgUY+abh0KCAcOsAIOQwx2PftAbLEPRA7mzXA/CjXRxzz0MC225pR/hx02Vf2Ag2x33kU4=";
258
259
    it('should verify using bitcoinjs-lib', function(done) {
260
        client.verifyMessage(message, address, signature, function(err, result) {
261
            assert.ifError(err);
262
            assert.ok(result);
263
            done();
264
        });
265
    });
266
267
    it('should handle errors nicely', function(done) {
268
        signature = "rubensayshi";
269
        client.verifyMessage(message, address, signature, function(err, result) {
0 ignored issues
show
Unused Code introduced by
The parameter result is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
270
            assert.ok(err);
271
            done();
272
        });
273
    });
274
275
    it('should verify using API', function(done) {
276
        client.client.post("/verify_message", null, {message: message, address: address, signature: signature}, function(err, result) {
277
            assert.ifError(err);
278
            assert.ok(result);
279
            done();
280
        });
281
    });
282
});
283
284
describe('send raw', function() {
285
    /**
286
     * @type APIClient
287
     */
288
    var client = blocktrail.BlocktrailSDK({
289
        apiKey : process.env.BLOCKTRAIL_SDK_APIKEY || "EXAMPLE_BLOCKTRAIL_SDK_NODEJS_APIKEY",
290
        apiSecret : process.env.BLOCKTRAIL_SDK_APISECRET || "EXAMPLE_BLOCKTRAIL_SDK_NODEJS_APISECRET",
291
        testnet : true
292
    });
293
294
    var tx = "0100000001bee92b36d3092e492e858d1199e46b942b3bddcc4c98071f0d307acced6f7751000000006b48304502210087831790820bf8218dc8df38758660a6f1f54a54d5d45ab0c3384e5ace9253ad0220650bce47447094148d45ec5b9ce4e3008e00723f4de6edd677110b2ebf0ff3da012102d8aa27d34020a6eb06e424787dbbb60f2cf4250a5a1110ab9e15e68fe710abc5ffffffff0131244c00000000001976a914a8d7a8e6724cf3f8ffb92c376ecb0094c18cbaf588ac00000000";
295
296
    // note that -27 (already in blockchain) is only when it's unspent
297
    it("should report TX is already in blockchain", function(done) {
298
        client.sendRawTransaction(tx, function(err, result) {
299
            assert.ok(err);
300
            assert.ok(result.code === -27 || result.code === -25);
301
302
            done();
303
        });
304
    });
305
306
    it('should error decode failed', function(done) {
307
        client.sendRawTransaction(tx.substr(-2), function(err, result) {
308
            assert.ok(err);
309
            assert.equal(-22, result.code);
310
311
            done();
312
        });
313
    });
314
});
315
316